window.onload = function() {
    var btn = document.getElementById("all");
    btn.onclick = function() {
        var flag = this.checked;
        var items = document.getElementsByName("sub");
        for (var i = 0; i < items.length; i++) {
            items[i].checked = flag;//将所有item的状态设为全选按钮的状态
        }
    }

    var items = document.getElementsByName("sub");
    for (var i = 0; i < items.length; i++) {
        items[i].onclick = function() {//对每个item设置点击
            var number = 0;//记录选中的个数
            for (var j = 0; j < items.length; j++) {
                if (items[j].checked) {
                    number++;
                }
            }
            document.getElementById("all").checked = (items.length == number);
        }
    }
}

var currPage = 1
// 每页默认5条数据
var pageSize = 10
// 获取传递的参数  /showhero.html?currPage=2--?currPage=2
var search = location.search
// 按照=对参数分割
var arr = search.split("=")
// 第一次访问页面时，是没有传递参数的，只有在分页时才会传递参数
// 防止第一次出现问题
if(arr.length >1){
    currPage = arr[1].split("&")[0]
}

if (currPage > 0 && currPage < 100) {
    page = currPage;
} else {
    page = 1;
}

$.ajax({
    url: "http://localhost:8080/eval/evall",
    data:"currPage="+page+"&pageSize="+pageSize,
    type: "get",
    dataType: "json",
    success: function (data) {
        let html = ''

        $.each(data.data.content, function (index, value) {
            html += `<tr>
             <td >
                <input type="checkbox" name="sub">&nbsp;&nbsp
                <button value="${value.id}" data-id="${value.id}" data-toggle="modal" data-target="#myModal">
                <i class="fa fa-trash" title="Remove" ></i>
                </button>
             </td>
             <td>${value.orderNo}</td>
             <td>${value.evaluateLevel}</td>
             <td>${value.evaluateContent}</td>
             <td>${value.evaluateDate}</td>
              </tr>`
        })
        $("table").append(html)
        generatePage(data.data.totalPages, data.data.number+1)
    }
})

function generatePage(totalPages, number) {
    var html = ''
    if (number == 1) {
        html += `<li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>`
    } else {
        html += `<li><a href="http://localhost:63343/qiguai/cart/showEvaluate.html?currPage=${number - 1}&pageSize=5" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>`
    }
    // console.log(navigatePages)
    // 生成页码
    let i = 1
    for (i; i <= totalPages; i++) {
        if (number == i) {
            // 当前页class=active，设置选中效果
            html += `<li class="active"><a href="#">${i}</a></li>`
        } else {
            // showhero.html?
            // 不是当前页
            html += `<li><a href="http://localhost:63343/qiguai/cart/showEvaluate.html?currPage=${i}&pageSize=5">${i}</a></li>`
        }

    }
    //不可用
    if (number >= totalPages) {
        html += `<li class="disabled"><a href="#" aria-label="Next">
                    <span aria-hidden="true">&raquo;</span>
                </a></li>`
    } else {
        html += `<li><a href="http://localhost:63343/qiguai/cart/showEvaluate.html?currPage=${number + 1}&pageSize=5" aria-label="Next">
                    <span aria-hidden="true">&raquo;</span>
                </a></li>`
    }
    $("#ulNav").append(html)
}


var evalId;

$('#myModal').on('show.bs.modal', function (event) {
    // 触发模态框的按钮
    var button = $(event.relatedTarget) // Button that triggered the modal
    // 从data-*内容中获取数据
    evalId = button.data('id') // Extract info from data-* attributes
    // 获取到modal 模态框对象
    var modal = $(this)
    modal.find('.modal-title').text('评论删除')

})


$("#myModal .modal-footer button").eq(1).click(function (){
    $.getJSON("http://localhost:8080/eval/delOneEval?evaluateId=" + evalId, data => {
        //location.reload();
        // if(data==1){
        //     location.reload();
        // } else {
        //     alert("删除失败")
        // }
    })
    location.reload();
    $('#myModal').modal('toggle')
})



var delids


function fre() {

    delids = $("input:checkbox[name='sub']:checked").map(function (index, elem) {
        return $(elem).next().val()
    }).get().join(',');

}
function delChos() {
    fre()
    //alert(delids)
    $.ajax({
        url: "http://localhost:8080/eval/delEvals",
        type: "get",
        data: "ids=" + delids,
        dataType: "json",
        success: function (data) {
            // if (data.data > 0) {
            //     alert("删除评论成功")
            //     location.reload();
            // } else {
            //     alert("删除评论失败")
            // }

        }
    })
    location.reload();
}

function subscribeBtn() {
    alert("订阅成功！")
}